﻿using System;
using System.ComponentModel;

namespace FscNET.Apm
{
    public sealed class EventApmOperation<TEventArgs>
    {
        // Fields
        private readonly AsyncCallback m_callback;
        private readonly object m_state;

        // Methods
        internal EventApmOperation(AsyncCallback callback, object state)
        {
            m_callback = callback;
            m_state = state;
        }

        public void EventHandler(object sender, TEventArgs e)
        {
            AsyncResult<TEventArgs> result = new AsyncResult<TEventArgs>(m_callback, m_state);
            AsyncCompletedEventArgs args = e as AsyncCompletedEventArgs;
            if ((args != null) && (args.Error != null))
            {
                result.SetAsCompleted(args.Error, false);
            }
            else
            {
                result.SetAsCompleted(e, false);
            }
        }
    }

}
